﻿using SuperJoe.Framework.Core.Exceptions;
using SuperJoe.Framework.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperJoe.Framework.UnitOfWorkSql
{
    public static class UnitOfWorkExtension
    {
        public static void ExecuteInTran(this IUnitOfWork uow, Action action)
        {
            try
            {
                uow.Begin();

                action();

                uow.End();
            }
            catch (UserFriendlyException uEx)
            {
                uow.Rollback();
                ThrowHelper.ThrowFriendlyException(uEx.FriendlyMessage, uEx.FriendlyMessage, uEx);
            }
            catch (Exception ex)
            {
                uow.Rollback();
                throw new Exception(ex.Message, ex);
            }
        }
        public static TDAL ResolveDAL<TDAL>(this IUnitOfWork uow)
        {
            return IocManager.Container.Resolve<TDAL>(parameters: new List<TypedConstructorParameter>() {
                new TypedConstructorParameter(typeof(IUnitOfWork), uow),
            });
        }
    }
}
